from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, JSON, Boolean
from sqlalchemy.orm import relationship
from datetime import datetime
from .database import Base

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    created_at = Column(DateTime, default=datetime.now)
    last_login = Column(DateTime, nullable=True)
    processes = relationship("Process", back_populates="creator")

class Process(Base):
    __tablename__ = "processes"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String)
    description = Column(String, nullable=True)
    nodes = Column(JSON)  # 存储流程节点数据
    created_at = Column(DateTime, default=datetime.now)
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    created_by = Column(Integer, ForeignKey("users.id"))
    status = Column(String, default="stopped")

    creator = relationship("User", back_populates="processes")

class SystemSettings(Base):
    __tablename__ = "system_settings"

    id = Column(Integer, primary_key=True)
    # 基本设置
    system_name = Column(String, default="TWRPA 智能RPA系统")
    api_url = Column(String, default="http://localhost:8000")
    log_level = Column(String, default="info")
    auto_backup = Column(Boolean, default=True)

    # 通知设置
    email_enabled = Column(Boolean, default=False)
    smtp_server = Column(String, nullable=True)
    smtp_port = Column(Integer, default=587)
    sender_email = Column(String, nullable=True)
    email_password = Column(String, nullable=True)

    # 安全设置
    password_expire_days = Column(Integer, default=90)
    login_lock_enabled = Column(Boolean, default=True)
    max_login_attempts = Column(Integer, default=5)
    lock_duration = Column(Integer, default=30)

    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    updated_by = Column(Integer, ForeignKey("users.id"), nullable=True) 